package com.logic.landseaserver.models.billcancel.ws;

import java.text.ParseException;
import java.util.Map;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.logic.common.enums.HttpCodeEnum;
import com.logic.common.util.CommonResult;
import com.logic.common.util.CommonResultMap;
import com.logic.landseaserver.common.exception.LandseaException;
import com.logic.landseaserver.common.util.LoggerHelper;
import com.logic.landseaserver.models.billcancel.bean.MeterReadCancelReq;
import com.logic.landseaserver.models.billcancel.bean.NoneMeterReadCancelReq;
import com.logic.landseaserver.models.billcancel.bean.ReadBillCancelReq;
import com.logic.landseaserver.models.billcancel.service.BillCancelService;
import com.logic.landseaserver.models.prepayment.service.AutoPayBillService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 
 *
 * 项目名称：system-server</br>
 * 类名称：BillCancelRest</br>
 * 类描述：账单勾销</br>
 * 创建人：Aaron</br>
 * 创建时间：2017年7月17日 下午2:35:52</br>
 * 
 * @version 1.0
 *
 */
@Path("/billcancel")
@Api(value = "账单勾销")
public class BillCancelRest
{
    private static final Logger LOGGER = LoggerFactory.getLogger(BillCancelRest.class);
    
    @Autowired
    private BillCancelService billCancelService;
    
    @Autowired
    private AutoPayBillService autoPayBillService;
    
    @POST
    @Path("/readBillCancel")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "实时账单勾销")
    public Response readBillCancel(@ApiParam(value = "param", required = true) ReadBillCancelReq readBillCancelReq)
    {
        CommonResultMap map = new CommonResultMap();
        try
        {
            Integer contractId = billCancelService.readBillCancel(readBillCancelReq);
            
            LOGGER.info("readBillCancel|实时账单勾销成功，开始进行自动余额抵扣.contractId=" + contractId);
            
            // 勾销之后，进行自动抵扣
            autoPayBillService.autoPayByContract(contractId);
            
            map.setResult(CommonResult.SUCCESS,"readBill  Cancel success");
            
        }
        catch (LandseaException e)
        {
            LoggerHelper.handleException(LOGGER, "readBillCancel", e, map);
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    @POST
    @Path("/noneMeterReadCancel")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "非抄表月结账单勾销")
    public Response noneMeterReadCancel(@ApiParam(value = "param", required = true) NoneMeterReadCancelReq req)
    {
        LOGGER.info("noneMeterReadCancel|非抄表月结账单勾销|" + req.toString());
        
        CommonResultMap map = new CommonResultMap();
        try
        {
            billCancelService.noneMeterReadCancel(req);
            map.setResult(CommonResult.SUCCESS,"noneMeterRead  Cancel success");
        }
        catch (LandseaException e)
        {
            LoggerHelper.handleException(LOGGER, "noneMeterReadCancel", e, map);
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    

    @POST
    @Path("/meterReadCancel")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "抄表月结账单勾销")
    public Response meterReadCancel(@ApiParam(value = "param", required = true) MeterReadCancelReq req)
    {
        LOGGER.info("meterReadCancel|抄表月结账单勾销|" + req.toString());
        
        CommonResultMap map = new CommonResultMap();
        try
        {
            billCancelService.meterReadCancel(req);
            map.setResult(CommonResult.SUCCESS,"meterRead  Cancel success");
        }
        catch (LandseaException e)
        {
            LoggerHelper.handleException(LOGGER, "meterReadCancel", e, map);
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    //查询前两次查表读数
    @GET
    @Path("/getMeterRead/{monthlyBillId}")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "获取monthlyBillId") 
    public Response getMeterRead(@ApiParam(value = "param", required = true) @PathParam("monthlyBillId") Integer monthlyBillId)
    {
        CommonResultMap map = new CommonResultMap();
        try
        {
            Map<String, Object> meterMap = billCancelService.getMeterRead(monthlyBillId);
            map.setData(meterMap);
            map.setResult(CommonResult.SUCCESS);
        }
        catch (LandseaException e)
        {
            LoggerHelper.handleException(LOGGER, "meterReadCancel", e, map);
        }
        catch (ParseException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    //判断是否有滞纳金
    @GET
    @Path("/getLateFeeMonthMap/{contractId}")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "查询合同下所有账单月是否有滞纳金")
    public Response getLateFeeMonthMap(@ApiParam(value = "param", required = true) @PathParam("contractId") Integer contractId)
    {
        CommonResultMap map = new CommonResultMap();
        try
        {
            Map<String,String> resultMap = billCancelService.getLateFeeMonthMap(contractId);
            map.setData(resultMap);
            map.setResult(CommonResult.SUCCESS, "lateFee");
        }
        catch (Exception e)
        {
            LOGGER.error("lateFee error", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

}
